package com.tongtech.proxy.core.rescue;

public class Dcramc {
    public static int maxPathSum(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dp = new int[m][n];

        // 初始化dp[0][0]
        dp[0][0] = matrix[0][0];

        // 初始化dp[0][1]到dp[0][n-1]
        for (int j = 1; j < n; j++) {
            dp[0][j] = dp[0][j - 1] + matrix[0][j];
        }

        // 初始化dp[1][0]到dp[m-1][0]
        for (int i = 1; i < m; i++) {
            dp[i][0] = dp[i - 1][0] + matrix[i][0];
        }

        // 计算dp[i][j]
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + matrix[i][j];
            }
        }

        // 返回dp[m-1][n-1]
        return dp[m - 1][n - 1];
    }
}
